package com.ld.security.server.blog.modules.cms.api;

import com.ld.security.auth.shiro.util.LoginUtil;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.base.po.EntityPo;
import com.ld.security.common.mybatis.base.po.IdPo;
import com.ld.security.common.mybatis.base.po.PagePo;
import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.mybatis.base.vo.PageVo;
import com.ld.security.common.tomcat.anno.AjaxLogKey;
import com.ld.security.common.tomcat.base.api.SuperApi;
import com.ld.security.server.blog.modules.cms.entity.CmsContentEntity;
import com.ld.security.server.blog.modules.cms.service.ICmsContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
@Api(tags="内容Api管理")
@RequestMapping("/api/cmsContent")
public class CmsContentApi  extends SuperApi<CmsContentEntity>{

        @Autowired
        ICmsContentService iCmsContentService;

    @AjaxLogKey
    @Override
    public DataVo save(@Valid @RequestBody EntityPo<CmsContentEntity> po) {
        CmsContentEntity cmsContentEntity=   po.getEntity();

        if(StringUtil.isNull(cmsContentEntity.getAuthorId())){
            String userId = LoginUtil.getUserId();
            cmsContentEntity.setAuthorId(userId);
        }
        if(StringUtil.isNull(po.getEntity().getCollectId())){
            po.getEntity().setCollectId(CmsContentEntity.content_type);
        }
        return super.save(po);
    }


    @Override
    public DataVo<PageVo<CmsContentEntity>> page(@RequestBody @Valid PagePo<CmsContentEntity> po) {
        if(StringUtil.isNull(po.getQuery().getCollectId())){
            po.getQuery().setCollectId(CmsContentEntity.content_type);

        }

        return super.page(po);
    }

    @ApiOperation("获取文章数据")
        @RequestMapping(value = "getArticle",method = RequestMethod.POST)
        public DataVo getArticle(@RequestBody @Valid IdPo po){
                CmsContentEntity cmsContentEntity =  iCmsContentService.getArticle(po.getId());
                return DataVo.of(cmsContentEntity);
        }
}